package chapter08

object Test04_MatchObject {
  def main(args: Array[String]): Unit = {
    val alice = new Student("alice", 19)

    val result =alice match {
      case Student("alice",19) =>"alice"+alice.name
      case _ => "else"
    }
  }
}


// 匹配对象实例

class Student(val name:String,val age:Int){

}

// 定义伴生对象
object Student {
  def apply(name:String,age:Int):Student = new Student(name,age)
  // 要做模式匹配，必须要实现unapply，用来对对象属性进行拆解

  def unapply(student: Student):Option[(String,Int)] ={
    if(student == null){
      None
    }else{
      Some((student.name,student.age))
    }
  }

}
